﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Data.SqlTypes;
using System.Text;

using CE.Database;
public partial class Admin_user_ViewUserBrowserInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("~/Admin/AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            if (Request.Params["ViewedId"] != null)
            {
                Int64 _intOutput = 0;
                if (Int64.TryParse(Request.Params["ViewedId"].ToString(), out _intOutput))
                {
                    //lblBrowserInfo.Text = GetBrowserInfo(_intOutput);
                    dvBrowserInfo.DataSource = GetBrowserInfo(_intOutput);
                    dvBrowserInfo.DataBind();
                }
            }
        }
    }

    //private string GetBrowserInfoXml(Int64 ViewedId)
    //{
    //    MyDatabase _mydb = new MyDatabase();
    //    string _spName = "[sprocUserViewedHistoryBrowserInfoDetailSelectSingleItemByViewedID]";

    //    SortedList _spList = new SortedList();
    //    _spList.Add("@ViewedId", ViewedId);
    //    //DataTable _dtReturn = new DataTable("BrowserInfo");
    //    SqlDataReader _drXmlBrowerInfo;
    //    _drXmlBrowerInfo = _mydb.GetSqlDataReaderBySP(_spName, _spList);

    //    string xmlString = string.Empty;
    //    SqlXml sqlXmlValue = null;
    //    XmlReader _xmlReader = null;
    //    StringBuilder _strBuilder = new StringBuilder();
    //    if (_drXmlBrowerInfo != null && _drXmlBrowerInfo.Read())
    //    {
    //        sqlXmlValue = _drXmlBrowerInfo.GetSqlXml(0);
    //        _drXmlBrowerInfo.Close();
    //    }

    //    if (sqlXmlValue != null)
    //    {
    //        _xmlReader = sqlXmlValue.CreateReader();

    //        while (_xmlReader.Read())
    //        {
    //            _strBuilder.Append(_xmlReader.ReadString());
    //            _strBuilder.Append("<br/>");
    //        }
    //        _xmlReader.Close();
    //    }
    //    else
    //    {
    //        _strBuilder.Append("No record.");
    //    }

    //    return _strBuilder.ToString();
    //}

    private DataTable GetBrowserInfo(Int64 ViewedId)
    {
        MyDatabase _mydb = new MyDatabase();
        string _spName = "[sprocUserViewedHistoryBrowserInfoDetailSelectSingleItemByViewedID]";

        SortedList _spList = new SortedList();
        _spList.Add("@ViewedId", ViewedId);
        //DataTable _dtReturn = new DataTable("BrowserInfo");
        DataTable _dtXmlBrowerInfo;
        _dtXmlBrowerInfo = _mydb.GetDataTableBySP(_spName, _spList);


        string xmlString = string.Empty;
        XmlReader _xmlReader = null;
        if (_dtXmlBrowerInfo != null && _dtXmlBrowerInfo.Rows.Count > 0)
        {
            _xmlReader = XmlTextReader.Create(new System.IO.StringReader(_dtXmlBrowerInfo.Rows[0][0].ToString()));
        }


        //System.IO.StringReader _sr = new System.IO.StringReader(xmlString);
        DataTable _dt = new DataTable("Test");
        _dt.Columns.Add("Browser", Type.GetType("System.String"));
        _dt.Columns.Add("Type", Type.GetType("System.String"));
        _dt.Columns.Add("Version", Type.GetType("System.String"));
        _dt.Columns.Add("MajorVersion", Type.GetType("System.String"));
        _dt.Columns.Add("MinorVersionString", Type.GetType("System.String"));
        _dt.Columns.Add("JavaScript", Type.GetType("System.String"));
        _dt.Columns.Add("VBScript", Type.GetType("System.String"));
        _dt.Columns.Add("ActiveXControls", Type.GetType("System.String"));
        _dt.Columns.Add("Frames", Type.GetType("System.String"));
        _dt.Columns.Add("Tables", Type.GetType("System.String"));
        _dt.Columns.Add("Cookies", Type.GetType("System.String"));
        _dt.Columns.Add("ClrVersion", Type.GetType("System.String"));
        _dt.Columns.Add("JavaApplets", Type.GetType("System.String"));
        _dt.Columns.Add("Win16", Type.GetType("System.String"));
        _dt.Columns.Add("Win32", Type.GetType("System.String"));
        _dt.Columns.Add("Platform", Type.GetType("System.String"));
        _dt.Columns.Add("UserAgent", Type.GetType("System.String"));
        _dt.Columns.Add("UserHostAddress", Type.GetType("System.String"));
        _dt.Columns.Add("UserHostName", Type.GetType("System.String"));
        _dt.Columns.Add("IsSecureConnection", Type.GetType("System.String"));
        _dt.Columns.Add("AnonymousID", Type.GetType("System.String"));
        _dt.Columns.Add("ContentLength", Type.GetType("System.String"));
        _dt.Columns.Add("ContentType", Type.GetType("System.String"));
        _dt.Columns.Add("HttpMethod", Type.GetType("System.String"));
        //_dt.ReadXml(_sr);
        int i = 0;
        DataRow _drNew = _dt.NewRow();
        if (_xmlReader.Read())
        {
            while (_xmlReader.Read())
            {
                if (i < 24)
                {
                    _drNew[i++] = _xmlReader.ReadString();
                }
            }
        }
        _dt.Rows.Add(_drNew);
        return _dt;
        //SqlXml sqlXmlValue = null;
        //XmlReader _xmlReader = null;

        //StringBuilder _strBuilder = new StringBuilder();
        //if(_dtXmlBrowerInfo != null && _dtXmlBrowerInfo.Rows.Count > 0)
        //{
        //    sqlXmlValue = _dtXmlBrowerInfo.Rows[0][0].ToString();
        //}

        //if (sqlXmlValue != null)
        //{
        //    _xmlReader = sqlXmlValue.CreateReader();

        //    while (_xmlReader.Read())
        //    {
        //        _strBuilder.Append(_xmlReader.ReadString());
        //        _strBuilder.Append("<br/>");
        //    }
        //    _xmlReader.Close();
        //}
        //else
        //{
        //    _strBuilder.Append("No record.");
        //}

        //return _strBuilder.ToString();
    }

}
